package com.linwen.excise.leetcode;

import java.util.HashSet;

/**
 * @ClassName _141环形链表
 * @Description TODO
 * @Author zero
 * @DATE 2024/4/21 10:00 PM
 * @Version 1.0
 */
public class _141环形链表 {
    public static void main(String[] args) {

    }
    public static boolean hasCycle(ListNode head) {
        if(head==null){
            return false;
        }
        HashSet<ListNode> hashSet = new HashSet<>();
        while (head!=null){
            if(hashSet.contains(head)){
                return true;
            }
            hashSet.add(head);
            head = head.next;
        }
        return false;
    }

    public static boolean hasCycle2(ListNode head) {
        if(head==null){
            return false;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (slow!=fast){
            if(fast==null||fast.next==null){
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
        }

        return true;
    }
}
